package com.juanpi.order.configuration;

import com.juanpi.order.configuration.bean.HeaderParam;
import com.juanpi.order.configuration.bean.UserConfig;
import com.juanpi.order.configuration.dao.HeaderParamMapper;
import com.juanpi.order.configuration.dao.UserConfigMapper;
import com.juanpi.yinhe.configuration.ConfigService;
import com.juanpi.yinhe.configuration.Shoe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xueqing
 *         Created on 2017/2/17.
 */

public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private UserConfigMapper userConfigMapper;

    @Autowired
    private HeaderParamMapper headerParamMapper;

    @Value("${application.name}")
    private String evn ;

    @Override
     public List<Shoe> findAllShoes() {
        List<Shoe> result = new ArrayList<>();
        List<UserConfig> userConfigs = userConfigMapper.findAllUserConfigs();
        for(UserConfig userConfig:userConfigs){
            if(!evn.equals(userConfig.getEvn())){
                continue;
            }
            Shoe shoe = new Shoe();
            shoe.setTopic(userConfig.getTopic());
            shoe.setGroupId(userConfig.getGroupId());
            shoe.setService(userConfig.getKafkaService());
            shoe.setUrl(userConfig.getCallbackUrl());
            List<HeaderParam> headerParams = headerParamMapper.queryByUserConfigId(userConfig.getId());
            if(headerParams.size()!=0){
                Map<String,String> header = new HashMap<>();
                for(HeaderParam headerParam:headerParams){
                    header.put(headerParam.getParamKey(),headerParam.getParamValue());
                    shoe.setHeader(header);
                }
            }
            result.add(shoe);
        }
        return result;
    }
}
